<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Yun;
class YunController extends Controller
{
    //
    public function yun_add(Request $request,Yun $yun){
        if($request->isMethod('post')){
            $data = $request->except('_token');
            if(empty($request->file('img'))){
                return back()->withErrors(['请添加封面图片']);
            }else{
                $data['img'] = $request->file('img')->store(date('Y-m-d'));
            }
            $data['yuncheng'] = $data['content'];
            $result = $yun->create($data);
            if($result != false){
                session()->flash('success','添加成功');
                return redirect()->back();
            }else{
                session()->flash('success','添加失败');
                return redirect()->back();
            }
        }else {
            return view('Admin/yun/yun_add');
        }
    }
    //列表
    public function yun_list(Yun $yun){
        $data = $yun->get();
        return view('Admin/yun/yun_list',['data'=>$data]);
    }
    //修改
    public function yun_upd(Yun $yun,$id){
        $data = $yun->where('id',$id)->get();
        return view('Admin.yun.yun_upd',['data'=>$data]);
    }
    //修改
    public function update(Request $request,Yun $yun){
        $data = $request->except('_token');
        $where = ['id'=>$data['id']];
        if(!empty($data['content'])){
            $re['yuncheng'] = $data['content'];
        }
        if(!empty($request->file('img'))){
            $re['img'] = $request->file('img')->store(date('Y-m-d'));
        }
        $re['date'] = $data['date'];
        $result = $yun->where($where)->update($re);
        if($result != false){
            session()->flash('success','修改成功');
            return redirect()->back();
        }else{
            session()->flash('success','修改失败');
            return redirect()->back();
        }
    }
}
